package fasilkom.antz.sprite;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;

import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;

import fasilkom.antz.Stage;


public class PlayingProfile extends SpriteGroup {
	
	private Sprite antSaved;
	private FontSprite antSavedText;
	private Sprite turn;
	private FontSprite turnText;
	private Sprite passButton;
	
	private Sprite missionButton;
	
	private BufferedImage flag0, flag1;
	private BufferedImage passButtonEn, passButtonDis;
	
	private Stage scene;
	private boolean passButtonEnabled;

	public PlayingProfile(Stage scene, int x, int y) {
		super("Playing Profile Group");
		
		this.scene = scene;
		/*GameFont font = scene.fontManager.getFont(scene.getImages
		("assets/font-small.png", 13, 6), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789   .,;:!?\"-/&`' ");*/

		GameFont font = scene.fontManager.getFont(new Font("Kristen ITC", Font.BOLD, 14));
		
		// Initialize components
		
		antSaved = new Sprite(scene.getImage("assets/icon-ant-saved.png"), x, y-30);
		antSavedText = new FontSprite("", font, 0, new Color(0x845833));
		antSavedText.setLocation(x + 30, y - 32);
		setAntSaved(scene.main.antSaved);
		
		flag0 = scene.getImage("assets/icon-flag-0.png");
		flag1 = scene.getImage("assets/icon-flag-1.png");
		turn = new Sprite(flag1, x, y + 10);
		turnText = new FontSprite("", font, 0, new Color(0x845833));
		turnText.setLocation(x + 30, y + 16);
		
		passButton = new Sprite(x-10, y + 60);
		passButtonEn = scene.getImage("assets/pass2.png");
		passButtonDis = scene.getImage("assets/pass4.png");
		setPassButtonEnabled(true);
		
		missionButton = new Sprite(scene.getImage("assets/mission.png"),x-10, y + 90);
		
		// Add to this group
		
		add(antSaved);
		add(antSavedText);
		add(turn);
		add(turnText);
		add(passButton);
		add(missionButton);
	}

	public void setAntSaved(int count) {
		antSavedText.setText("Saved:\n" + count);
	}
	
	private void setPlayerTurn(boolean isPlayer) {
		if (isPlayer) {
			turn.setImage(flag1);
			turnText.setText("Player");
		} else {
			turn.setImage(flag0);
			turnText.setText("Enemy");
		}
		setPassButtonEnabled(isPlayer);
	}
	
	@Override
	public void update(long elapsedTime) {
		super.update(elapsedTime);
		
		// Cek passButton click
		if (passButtonEnabled ) {
			int x0 = (int) passButton.getScreenX();
			int y0 = (int) passButton.getScreenY();
			int x1 = x0 + passButton.getWidth();
			int y1 = y0 + passButton.getHeight();
			if (scene.click() && scene.getMouseX() > x0 && scene.getMouseX() < x1
					&& scene.getMouseY() > y0 && scene.getMouseY() < y1) {
				scene.switchTurn();
			}
		}
		
		buttonClicked(missionButton);
		buttonClicked(scene.closeButton);
		
		// Cek turn
		setPlayerTurn(scene.isPlayerTurn());
	}

	public void setPassButtonEnabled(boolean passButtonEnabled) {
		this.passButtonEnabled = passButtonEnabled;
		passButton.setImage(passButtonEnabled ? passButtonEn : passButtonDis);
	}

	public boolean isPassButtonEnabled() {
		return passButtonEnabled;
	}
	
	public void buttonClicked(Sprite button){
		
		int x0 = (int) button.getScreenX();
		int y0 = (int) button.getScreenY();
		int x1 = x0 + button.getWidth();
		int y1 = y0 + button.getHeight();
		
		if (scene.click() && scene.getMouseX() > x0 && scene.getMouseX() < x1
				&& scene.getMouseY() > y0 && scene.getMouseY() < y1 ) {
			scene.mission.setActive(!scene.mission.isActive());
		}
	}
}
